import pandas as pd
# from new_func.print_log import log
from new_func.tools import Tool
from PyQt5.QtCore import pyqtSignal
from resources.excel_loding import Excel_Form
from PyQt5.QtWidgets import QFrame
from PyQt5.QtWebEngineWidgets import QWebEngineView


class ExcelWidget(QFrame, Excel_Form):
    chart = pyqtSignal()

    def __init__(self, text, parent=None):
        super(ExcelWidget, self).__init__(parent=parent)
        self.setupUi(self)
        self.setObjectName(text.replace(' ', '-'))

        self.tool = Tool()
        self.chart_view = QWebEngineView()
        self.vline_chart.addWidget(self.chart_view)
        self.currentSheet = None
        self.signals()

    def signals(self):
        """按钮触发"""
        self.loding_file.clicked.connect(lambda: self.tool.handle_path(self.loding_line))
        self.loding_line.textChanged.connect(self.suffix_info)

    def suffix_info(self):
        """检测后缀"""
        self.tool.get_suffix(self.loding_line)
        if self.tool.suffix not in ['.xlsx']:
            self.tool.show_error(self, "❗️文件错误", "文件后缀错误")
        self.read_file()

    def read_file(self):
        """读取csv以及excel，写入combox列"""
        if self.tool.path:
            self.header_box1_clear()
            self.header_box2_clear()
            self.header_sheet1_clear()
            self.header_sheet2_clear()
            if self.tool.suffix == ".xlsx":
                self.df = pd.ExcelFile(self.tool.path)
                self.loding_sheet1.addItems(list(self.df.sheet_names))
                self.loding_sheet2.addItems(list(self.df.sheet_names))
                self.load_sheet(list(self.df.sheet_names)[0])  # 默认加载第一个表单
                self.load_excel_box1()
                self.load_excel_box2()
                self.loding_sheet1.currentTextChanged.connect(self.update_sheet_box1)
                self.loding_sheet2.currentTextChanged.connect(self.update_sheet_box2)

    def header_box1_clear(self):
        """检查并清除combox1"""
        if self.loding_box1.count() > 0:
            self.loding_box1.clear()

    def header_box2_clear(self):
        """检查并清除combox2"""
        if self.loding_box2.count() > 0:
            self.loding_box2.clear()

    def load_excel_box1(self):
        """加载combox1的值"""
        self.loding_box1.addItems(list(self.currentSheet.columns))

    def load_excel_box2(self):
        """加载combox2的值"""
        self.loding_box2.addItems(list(self.currentSheet.columns))

    def header_sheet1_clear(self):
        """检查并清楚sheet1的值"""
        if self.loding_sheet1.count() > 0:  # 如果有值，清除
            self.loding_sheet1.clear()

    def header_sheet2_clear(self):
        """检查并清楚sheet2的值"""
        if self.loding_sheet2.count() > 0:
            self.loding_sheet2.clear()

    def update_sheet_box1(self):
        """更新excel的第一个sheet"""
        self.header_box1_clear()
        self.load_sheet(self.loding_sheet1.currentText())
        self.load_excel_box1()

    def update_sheet_box2(self):
        """更新excel的第二个sheet"""
        self.header_box2_clear()
        self.load_sheet(self.loding_sheet2.currentText())
        self.load_excel_box2()

    def load_sheet(self, sheet_name):
        """加载指定表单"""
        self.currentSheet = pd.read_excel(self.df, sheet_name=sheet_name)

    def line_chart(self):
        """制作折线图"""
        sheet1, sheet2, y1, y2 = self.tool.get_widget_text(self.frame)
        height, width = self.frame_2.height() - 105, self.frame_2.width() - 85
        if sheet1 == sheet2:  # 加载excel
            if y1 == y2:
                x = self.currentSheet['time'].tolist()
                y1_data = self.currentSheet[y1].tolist()
                self.tool.one_chart(x=x, y1=y1_data, y1_name=y1, height=height, width=width)
                # log.info("\033[0;32m" + '作图成功' + "\033[0m")
                self.chart.emit()
                print("作图成功")
            else:
                x = self.currentSheet['time'].tolist()
                y1_data = self.currentSheet[y1].tolist()
                y2_data = self.currentSheet[y2].tolist()
                self.tool.two_chart(x=x, y1=y1_data, y1_name=y1, y2=y2_data, y2_name=y2, height=height, width=width)
                # log.info("\033[0;32m" + '作图成功' + "\033[0m")
                self.chart.emit()
                print("作图成功")
        else:
            x = self.currentSheet['time'].tolist()
            self.load_sheet(sheet1)
            y1_data = self.currentSheet[y1].tolist()
            self.load_sheet(sheet2)
            y2_data = self.currentSheet[y2].tolist()
            self.tool.two_chart(x=x, y1=y1_data, y1_name=y1, y2=y2_data, y2_name=y2, height=height, width=width)
            # log.info("\033[0;32m" + '作图成功' + "\033[0m")
            self.chart.emit()
            print("作图成功")
